'''
Created on Apr 3, 2013

@author: david
'''
from app.Ranger.actions.home import Home
"""
-dkc

Initial phase does not tie users to dealers so this
has been defeated (commented out) in this Action and 
can be added back in a future rehash. <P1> Tags 
indicate this code...
"""

class Authorize(Home):
    """
    Default Action 
    spawns index.gen
    """
    def execute(self):
        from app.Ranger.agents import AccessAgent
        import json
        import logging

        # set up for error
        rv = {'result': 500, "message": "Invalid Credentials"}
        # but check anyway
        req = self.request
        logging.info("~~ %s" % (str(req.getParameterNames())))

        uid = req.getParameter("uid", 'none').lower()
        pwd = req.getParameter("pwd", 'none')

        # validate credentials
        acct = AccessAgent().getAccount(uid, pwd)

        # yea or nea
        if not acct:
            # no go
            logging.warn("!! Login Fail %s" % uid)

            # audit
            logging.info("-- Denied: %s" % uid)

        else:
            # save info for submits
            req.session['acct'] = acct.aid
            req.session['uid'] = uid

            rv = {'result': 200, "message": "Ok", 'user': acct.toDict()}
            logging.info("~~ acct: %s" % str(rv))
            logging.info("++ Login: %s" % uid)

        # return result
        return (self.JSONOutput(json.dumps(rv)))